﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.HomeServer.Common.Client;
using Microsoft.HomeServer.Controls;
using Umbrella.Whs.Contracts;
using Umbrella.Whs.SharePoint;

namespace Umbrella.Whs.WhsAddin.ConsoleControls
{
    internal partial class UsersList : ConsoleControlBase
    {
        private readonly List<UserInfo> _users = new List<UserInfo>();
        private Website _web; 

        public UsersList(UmbrellaConsoleTab tab)
            : base(tab)
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Initialization start");

            try
            {
                InitializeComponent();

                // fill image list
                this.imageList.Images.Add(CommonImages.UserImage48x48);
                this.imageList.Images.Add(CommonImages.GuestUserImage48x48);

                // open sharepoint
                _web = new Website();

                // done
                if (_log.IsDebugEnabled)
                    _log.Debug("Initialization done");
            }
            catch (Exception e)
            {
                _log.Fatal("Failed to initialize", e);

                throw;
            }
        }

        private void UsersList_Load(object sender, EventArgs e)
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Loading start");

            try
            {
                // load users
                this.listView.SuspendLayout();

                this.listView.Items.Clear();

                foreach (var ue in _singletonService.Channel.GetAllUsers())
                {
                    _users.Add(ue);

                    // add to list
                    this.listView.Items.Add(ue.CreateItem());
                }

                this.listView.ResumeLayout();

                // done
                if (_log.IsDebugEnabled)
                    _log.Debug("Loading done");
            }
            catch (Exception x)
            {
                _log.Fatal("Loading failed", x);

                throw;
            }
        }
    }
}
